package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.bo.UserPxjlBo;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SysUserPxjl;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SysUserPxjlMapper;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.ISysUserPxjlService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.OrderItemUtils;

import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Description: 系统-用户培训记录
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Service
public class SysUserPxjlServiceImpl extends BaseServiceImpl<SysUserPxjlMapper, SysUserPxjl> implements ISysUserPxjlService {

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    @Override
    public IPage getPageList(Page<SysUserPxjl> page, UserPxjlBo sysUserPxjl, String year, boolean isSelectMe, OrderItemVo orderItemVo) {
        LambdaQueryWrapper<SysUserPxjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(sysUserPxjl.getFuserid()),SysUserPxjl::getFuserid,sysUserPxjl.getFuserid());
        queryWrapper.eq(StringUtils.isNotBlank(sysUserPxjl.getFpxjbxxid()),SysUserPxjl::getFpxjbxxid, sysUserPxjl.getFpxjbxxid());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPxjl.getFpxdd()),SysUserPxjl::getFpxdd,sysUserPxjl.getFpxdd());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPxjl.getFpxdj()),SysUserPxjl::getFpxdj,sysUserPxjl.getFpxdj());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPxjl.getFpxlx()),SysUserPxjl::getFpxlx,sysUserPxjl.getFpxlx());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPxjl.getFpxmc()),SysUserPxjl::getFpxmc,sysUserPxjl.getFpxmc());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPxjl.getFpxzjr()),SysUserPxjl::getFpxzjr,sysUserPxjl.getFpxzjr());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPxjl.getFusermc()),SysUserPxjl::getFusermc,sysUserPxjl.getFusermc());
        queryWrapper.like(StringUtils.isNotBlank(sysUserPxjl.getFpxly()),SysUserPxjl::getFpxly,sysUserPxjl.getFpxly());

        if (StringUtils.isNotBlank(year) && StringUtils.isBlank(sysUserPxjl.getFjlnd())) {
            sysUserPxjl.setFjlnd(year);
            year = "";
        }

        queryWrapper.like(StringUtils.isNotEmpty(sysUserPxjl.getFjlnd()), SysUserPxjl::getFjlnd, sysUserPxjl.getFjlnd());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPxjl.getFpxzsbh()),SysUserPxjl::getFpxzsbh,sysUserPxjl.getFpxzsbh());
        if (sysUserPxjl.getFpxkssj() != null){
            queryWrapper.ge(SysUserPxjl::getFpxkssj,sysUserPxjl.getFpxkssj());
        }
        if (sysUserPxjl.getFpxjssj() != null){
            queryWrapper.le(SysUserPxjl::getFpxjssj,sysUserPxjl.getFpxjssj());
        }
        if (StringUtils.isNotBlank(year)){
            queryWrapper.apply("to_char(fpxkssj,'yyyy') = {0}",year);
        }
        if (isSelectMe){
            CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
            if (currentUser != null){
                queryWrapper.eq(StringUtils.isNotBlank(currentUser.getId()),SysUserPxjl::getFuserid,currentUser.getId());
            }
        }
        if (StringUtils.isBlank(orderItemVo.getOrderItem())) {
            queryWrapper.orderByDesc(SysUserPxjl::getCreateTime);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        IPage<SysUserPxjl> pageList = page(page, queryWrapper);
        return pageList;
    }

    @Override
    public List<SysUserPxjl> getList(UserPxjlBo sysUserPxjl, String year, boolean isSelectMe) {
        LambdaQueryWrapper<SysUserPxjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(sysUserPxjl.getFuserid()),SysUserPxjl::getFuserid,sysUserPxjl.getFuserid());
        queryWrapper.eq(StringUtils.isNotBlank(sysUserPxjl.getFpxjbxxid()),SysUserPxjl::getFpxjbxxid, sysUserPxjl.getFpxjbxxid());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPxjl.getFpxdd()),SysUserPxjl::getFpxdd,sysUserPxjl.getFpxdd());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPxjl.getFpxdj()),SysUserPxjl::getFpxdj,sysUserPxjl.getFpxdj());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPxjl.getFpxlx()),SysUserPxjl::getFpxlx,sysUserPxjl.getFpxlx());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPxjl.getFpxmc()),SysUserPxjl::getFpxmc,sysUserPxjl.getFpxmc());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPxjl.getFpxzjr()),SysUserPxjl::getFpxzjr,sysUserPxjl.getFpxzjr());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPxjl.getFusermc()),SysUserPxjl::getFusermc,sysUserPxjl.getFusermc());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPxjl.getFjlnd()),SysUserPxjl::getFjlnd,sysUserPxjl.getFjlnd());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPxjl.getFpxzsbh()),SysUserPxjl::getFpxzsbh,sysUserPxjl.getFpxzsbh());
        if (sysUserPxjl.getFpxkssj() != null){
            queryWrapper.le(SysUserPxjl::getFpxkssj,sysUserPxjl.getFpxkssj());
        }
        if (sysUserPxjl.getFpxjssj() != null){
            queryWrapper.ge(SysUserPxjl::getFpxjssj,sysUserPxjl.getFpxjssj());
        }
        if (StringUtils.isNotBlank(year)){
            queryWrapper.apply("to_char(fpxkssj,'yyyy') = {0}",year);
        }
        if (isSelectMe){
            CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
            if (currentUser != null){
                queryWrapper.eq(StringUtils.isNotBlank(currentUser.getId()),SysUserPxjl::getFuserid,currentUser.getId());
            }
        }
        queryWrapper.orderByDesc(SysUserPxjl::getCreateTime);
        List<SysUserPxjl> pageList = list(queryWrapper);
        return pageList;
    }

    @Override
    public void saveBatchPxjl(List<SysUserPxjl> pxjlList) {

        if (CollectionUtil.isNotEmpty(pxjlList)){
            saveBatch(pxjlList);
        }
       /* for (int i=0; i<pxjlList.size(); i++){
            SysUserPxjl pxjl = pxjlList.get(i);
            List<SysUserPxjl> existList = list(new LambdaQueryWrapper<SysUserPxjl>()
                    .eq(SysUserPxjl::getFuserid, pxjl.getFuserid())
                    .eq(SysUserPxjl::getFjlnd, pxjl.getFjlnd())
                    .eq(SysUserPxjl::getFpxlx, pxjl.getFpxlx())
                    .eq(SysUserPxjl::getFpxmc, pxjl.getFpxmc()));
            if (CollectionUtil.isNotEmpty(existList)){
                SysUserPxjl oldPx = existList.get(i);
                oldPx.setFpxkssj(pxjl.getFpxkssj());
                updateById(oldPx);
            }else {
                save(pxjl);
            }
        }*/
    }

    @Override
    public String getTotalLearningHoursTheCurrentYear(String userid, String year) {
        if (StringUtils.isBlank(userid) || StringUtils.isBlank(year)){
            return "0";
        }

        List<SysUserPxjl> list = list(new LambdaQueryWrapper<SysUserPxjl>()
                .eq(SysUserPxjl::getFuserid, userid)
                .eq(SysUserPxjl::getFjlnd, year));

        if (CollectionUtil.isEmpty(list)){
            return "0";
        }

        BigDecimal totalLearningHours = new BigDecimal("0");
        for (SysUserPxjl pxjl : list){
            totalLearningHours = totalLearningHours.add(new BigDecimal(Optional.ofNullable(pxjl.getFpxxs()).orElseGet(()->"0")));
        }
        return totalLearningHours.toString();
    }

    @Override
    public SysUserPxjl addPxjl(SysUserPxjl bean) {
        bean.setId(GetUuIdUtils.ReplaceUuId());
        bean.setCreateTime(new Date());
        bean.setUpdateTime(new Date());
        bean.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
        bean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        save(bean);

        String operContent = "新增信息：";
        operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(LogConstant.LOGS_SYS_USER_PXJL,operContent,bean.getId(), "添加培训记录", SpcyConstant.LOGS_SYS_TYPE_XZ);

        return bean;
    }

    @Override
    public void updatePxjl(SysUserPxjl buffer) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        String id = buffer.getId();
        if (StringUtils.isEmpty(id)){
            throw new BusinessException("参数为空!");
        }

        SysUserPxjl updateBean = getById(id);
        if (updateBean == null){
            throw new BusinessException("没有相匹配数据！");
        }

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, BeanUtils.describe(buffer));
        if (buffer.getFpxkssj() != null){
            updateBean.setFpxkssj(buffer.getFpxkssj());
        }else {
            updateBean.setFpxkssj(null);
        }
        if (buffer.getFpxjssj() != null){
            updateBean.setFpxjssj(buffer.getFpxjssj());
        }else {
            updateBean.setFpxjssj(null);
        }
        updateById(updateBean);
    }
}
